#include "cylinderWindow.h"
#include "modelSoft.h"
#include "ui_cylinderWindow.h"
#include "BRep_Tool.hxx"



cylinderWindow::cylinderWindow(modelSoft* myModelSoft, CylinderFeature* m_cylinderFeature,QWidget *parent)
	:myModelSoft(myModelSoft)
	, cylinderFeature(m_cylinderFeature)
	,QWidget(parent)
	, ui(new Ui::cylinderWindowClass())
{
	ui->setupUi(this);
	ui->radius1Input->setText(QString::number(cylinderFeature->getRadius1()));
	ui->highInput->setText(QString::number(cylinderFeature->getHigh()));
	shapeID = cylinderFeature->getShapeId();
	//查找该Pipeline是否已存在，存在则获取，不存在则创建
	auto iter1 = myModelSoft->m_pipelineMap.find(cylinderFeature->getShapeId());
	//Handle(QIVtkSelectionPipeline) cubePipeline;
	if (iter1 == myModelSoft->m_pipelineMap.end()) {
	cylinderPipeline = myModelSoft->makeCylinder(cylinderFeature);
	cylinderPipeline->Actor()->SetPickable(false);
	}
	else {
		cylinderPipeline = iter1->second;
		edit = true;
		//已存在pipeline,高亮显示
		cylinderPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		myModelSoft->myView->getRenderWindow()->Render();
	}

	createConnections();
}

void cylinderWindow::createConnections() {
	
	connect(ui->confirmButton, &QPushButton::clicked, this, &cylinderWindow::makeCylinder_onClick);
	connect(ui->cancelButton, &QPushButton::clicked, this, &QWidget::close);
	connect(myModelSoft->currVertexSelector, &OccVertexSelector::sentVertex, this, &cylinderWindow::receivePos);
	connect(ui->vertexSelectButton, &QPushButton::clicked, this, &cylinderWindow::onClickVertexSelect);
	connect(ui->radius1Input, &QLineEdit::textChanged, this, &cylinderWindow::onRadius1Changed);
	connect(ui->highInput, &QLineEdit::textChanged, this, &cylinderWindow::onRadius2Changed);

}

void cylinderWindow::receivePos(const TopoDS_Shape& shape)
{
	const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
	// 获取顶点坐标
	if (!vertex.IsNull()) {
		point_pos = BRep_Tool::Pnt(vertex);
		cylinderFeature->setLocation(point_pos);
		cylinderPipeline = myModelSoft->updateCylinderPipelineShape(cylinderPipeline, cylinderFeature);
		cylinderPipeline->Actor()->SetPickable(false);
	}
	else {
		qDebug() << "null vertex";
	}

}

void cylinderWindow::onClickVertexSelect()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Vertex);
	myModelSoft->changeSelector(SM_Vertex);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void cylinderWindow::onRadius1Changed()
{
	cylinderFeature->setRadius1(ui->radius1Input->text().toDouble());
	cylinderPipeline = myModelSoft->updateCylinderPipelineShape(cylinderPipeline, cylinderFeature);
	cylinderPipeline->Actor()->SetPickable(false);

}

void cylinderWindow::onRadius2Changed()
{
	cylinderFeature->setHigh(ui->highInput->text().toDouble());
	cylinderPipeline = myModelSoft->updateCylinderPipelineShape(cylinderPipeline, cylinderFeature);
	cylinderPipeline->Actor()->SetPickable(false);

}

void cylinderWindow::closeEvent(QCloseEvent* event)
{
	if (!confirmCreate) {
		if (!cylinderPipeline.IsNull() && !edit) {
			myModelSoft->RemovePipelineFromRenderer(cylinderPipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
	}
	if (!cylinderPipeline.IsNull() && edit) {
		cylinderPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);
		myModelSoft->myView->getRenderWindow()->Render();
	}
}

double cylinderWindow::get_cylinder_radius1() {
	QString lengthText = ui->radius1Input->text();
	return lengthText.toDouble();
}

double cylinderWindow::get_cylinder_radius2() {
	QString widthText = ui->highInput->text();

	return widthText.toDouble();
}


void cylinderWindow::makeCylinder_onClick() {
	confirmCreate = true;
	cylinderPipeline->Actor()->SetPickable(true);
	if(!edit){
	QStandardItem* item1 = new QStandardItem(QString("圆柱体-%1").arg(cylinderPipeline->GetShapeID()));
	item1->setEditable(false);
	int row = myModelSoft->ui->model->rowCount();
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	}

	close();
}

cylinderWindow::~cylinderWindow()
{}
